# Verify conditions
ping -c 1 -W 1 8.8.8.8 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    ping -c 1 -W 1 www.baidu.com > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo Network unreachable.
        exit 1
    fi
fi

# Install software and tools
brew install unzip rar dos2unix git

# Install OpenCV
OPENCV_PACKAGE=OpenCV410-ArmSonoma.zip
if [ -f $OPENCV_PACKAGE ]; then
    sudo unzip -o -d / ./$OPENCV_PACKAGE
fi

# Install FFmpeg for development (Unnecessary for Appox SDK 5.1~)
FFMPEG_PACKAGE=ffmpeg-6.1.1-macos-arm64-gpl.zip
if [ -f $FFMPEG_PACKAGE ]; then
    sudo unzip $FFMPEG_PACKAGE -d /usr/local/lib/
fi

# Install Clang
if [ ! -d "/opt/clang-15" ]; then
    CLANG_PACKAGE=clang-15-arm-macos.zip
    if [ ! -f $CLANG_PACKAGE ]; then
        echo $CLANG_PACKAGE not found.
        exit 1
    fi

    sudo unzip -d /opt ./$CLANG_PACKAGE
fi

# Install XTools
if [ ! -d "/opt/xtools-uni/x86-windows10" ]; then
    UNI_X86_WINDOWS10_PACKAGE=20230521-x86-windows10.zip
    if [ ! -f $UNI_X86_WINDOWS10_PACKAGE ]; then
        echo $UNI_X86_WINDOWS10_PACKAGE not found.
        exit 1
    fi

    sudo unzip -d /opt/xtools-uni ./$UNI_X86_WINDOWS10_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/x86-ubuntu2204" ]; then
    UNI_X86_UBUNTU2204_PACKAGE=20230602-x86-ubuntu2204.zip
    if [ ! -f $UNI_X86_UBUNTU2204_PACKAGE ]; then
        echo $UNI_X86_UBUNTU2204_PACKAGE not found.
        exit 1
    fi

    sudo unzip -d /opt/xtools-uni ./$UNI_X86_UBUNTU2204_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/arm-ubuntu2204" ]; then
    UNI_ARM_UBUNTU2204_PACKAGE=20230602-arm-ubuntu2204.zip
    if [ ! -f $UNI_ARM_UBUNTU2204_PACKAGE ]; then
        echo $UNI_ARM_UBUNTU2204_PACKAGE not found.
        exit 1
    fi

    sudo unzip -d /opt/xtools-uni ./$UNI_ARM_UBUNTU2204_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/x86-macos1013" ]; then
    UNI_X86_MACOS1013_PACKAGE=20230531-x86-macos1013.zip
    if [ ! -f $UNI_X86_MACOS1013_PACKAGE ]; then
        echo $UNI_X86_MACOS1013_PACKAGE not found.
        exit 1
    fi

    sudo unzip -d /opt/xtools-uni ./$UNI_X86_MACOS1013_PACKAGE
fi

if [ ! -d "/opt/xtools-uni/arm-macos1203" ]; then
    UNI_ARM_MACOS1203_PACKAGE=20230522-arm-macos1203.zip
    if [ ! -f $UNI_ARM_MACOS1203_PACKAGE ]; then
        echo $UNI_ARM_MACOS1203_PACKAGE not found.
        exit 1
    fi

    sudo unzip -d /opt/xtools-uni ./$UNI_ARM_MACOS1203_PACKAGE
fi

# Install free .NET debugger
if [ ! -f "/usr/local/bin/netcoredbg" ]; then
    NETCOREDBG_PACKAGE=NetCoreDbg-3.1.0-ArmMacOS.zip
    if [ ! -f $NETCOREDBG_PACKAGE ]; then
        echo $NETCOREDBG_PACKAGE not found.
        exit 1
    fi

    sudo unzip -o -d / ./$NETCOREDBG_PACKAGE
fi

echo Done.
